<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\stage\controller;
use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

class Quanzhi extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'php_quanzhi';

    /**
     * 商品列表
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function index()
    {
        $this->title = '圈子列表';
        $db = Db::name($this->table);
        return parent::_list($db->order('status add'));
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        $data = ToolsService::arr2table($data);
    }
    /**
     * 查看圈子详情
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function edit()
    {
        if($_POST){
            $data = [
                'uid' =>$_POST['uid'],
                'title' => "发圈通知",
                'content' => "尊敬的用户你好！由于你发表的圈子内容涉及平台所不允许的内容，已被禁用。"
            ];
            Db::name('php_tongzhi')->insert($data);
            Db::name($this->table)->where(['id' => $_POST['id']])->update(['status' => 1]);
            $this->_form_result(999);
            die;
        }
        $this->title = '查看详情';
        //查询该圈子所属评论
        $pid = $_GET['id'];
        $f = "a.*,b.image,b.nickname,b.username";
        $pinglun = Db::name('php_quanzhi_pinglun')->alias('a')
            ->join('php_member b','a.q_uid = b.id')
            ->field($f)
            ->where(['a.q_id' => $pid])->select();

        foreach ($pinglun as $k => $v){
            $pinglun[$k]['huifu'] = false;
            if($v['q_hf_pid']){
               $huifu = Db::name('php_member')->where(['id' => $v['q_hf_pid']])->find();
                $pinglun[$k]['huifu'] = $huifu;
            }
        }

        //查询点赞
        $dianzan = Db::name('php_quanzhi_zan')->alias('a')
            ->join('php_member b','a.z_uid = b.id')
            ->field($f)
            ->where(['a.z_qun_id' => $pid])->select();

        $this->assign('pinglun',$pinglun);
        $this->assign('dianzan',$dianzan);


        return $this->_form($this->table, 'form');
    }

    /**
     * 删除商家
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");

    }

    /**
     * 商家禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $uid = $_GET['uid'];

        if (DataService::update($this->table)) {
            //发送通知到用户
            $data = [
                'uid' =>$uid,
                'title' => "发圈通知",
                'content' => "尊敬的用户你好！由于你发表的圈子内容涉及平台所不允许的内容，已被禁用。"
            ];
            Db::name('php_tongzhi')->insert($data);
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    /**
     * 商家启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    /**
     * 添加成功回跳处理
     * @param bool $result
     */
    protected function _form_result($result)
    {
        if ($result !== false) {
            list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('stage/quanzhi/index')];
            $this->success('数据保存成功！', "{$base}#{$url}?spm={$spm}");
        }
    }
}
